package com.scsj.supermarket.model;

import com.scsj.supermarket.base.DeliveryAddressBase;
import com.scsj.supermarket.bean.BaseBean;
import com.scsj.supermarket.bean.DeliveryAddressBean;

import dkmvp.net.ResponseCallback;
import dkmvp.net.ResponseHandler;
import dkmvp.net.RetryWithDelay;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 获取收货地址列表，删除收货地址，设置默认收货地址Model
 */

public class DeliveryAddressModel extends BaseModel implements DeliveryAddressBase.IModel {

    @Override
    public Subscription getDAlistData(RequestBody requestBody, final ResponseCallback<DeliveryAddressBean> callback) {

        Observable<DeliveryAddressBean> observable = service.GetDeliveryAddress(requestBody);

        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<DeliveryAddressBean>() {
                    @Override
                    public void call(DeliveryAddressBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    /**
     * 删除收货地址
     *
     * @param requestBody
     * @param callback
     * @return
     */
    @Override
    public Subscription getDeleteDAData(RequestBody requestBody, final ResponseCallback<BaseBean> callback) {
        Observable<BaseBean> observable = service.GetDeleteDeliveryAddress(requestBody);

        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<BaseBean>() {
                    @Override
                    public void call(BaseBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    /**
     * 设置默认收货地址
     */
    @Override
    public Subscription getSetDefaultDAData(RequestBody requestBody, final ResponseCallback<BaseBean> callback) {
        Observable<BaseBean> observable = service.SetDefaultDeliveryAddress(requestBody);

        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<BaseBean>() {
                    @Override
                    public void call(BaseBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

}
